package com.app.couponsharelife.ui.main.nativelife.activity

import android.text.Editable
import android.text.TextWatcher
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.utils.AppUtils
import kotlinx.android.synthetic.main.activity_goods_shoppay.*
import kotlinx.android.synthetic.main.base_title_white.*

/**
 *Created by xct on 2019/3/7
 *Description:本地生活店内支付页面
 */
class GoodsShopPayActivity:BaseActivity() {
    private var temp: CharSequence? = null    // 监听前的文本
    private var editStart: Int = 0      // 光标开始位置
    private var editEnd: Int = 0        // 光标结束位置
    override val viewLayoutId: Int
        get() = R.layout.activity_goods_shoppay

    override fun initViews() {
        AppUtils.initImmersionBar(this,true)
        basetitle_title.text="买单"
        basetitle_llclose.setOnClickListener { finish() }
        goodsShoppay_etPrice.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
                editStart = goodsShoppay_etPrice.selectionStart
                editEnd = goodsShoppay_etPrice.selectionEnd
                val arr = temp.toString().split("[.]".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                if (arr.size > 1) {
                    if (arr[1].length > 2) {
                        s.delete(editStart - 1, editEnd)
                        val tempSelection = editStart
                        goodsShoppay_etPrice.text=s
                        goodsShoppay_etPrice.setSelection(tempSelection)
                    }
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                temp=s
            }
        })
        goodsShoppay_etYhPrice.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
                editStart = goodsShoppay_etYhPrice.selectionStart
                editEnd = goodsShoppay_etYhPrice.selectionEnd
                val arr = temp.toString().split("[.]".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                if (arr.size > 1) {
                    if (arr[1].length > 2) {
                        s.delete(editStart - 1, editEnd)
                        val tempSelection = editStart
                        goodsShoppay_etYhPrice.text=s
                        goodsShoppay_etYhPrice.setSelection(tempSelection)
                    }
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                temp=s
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}